<?php

require_once 'Pipes/FileSystem/File/Finder.php';
require_once 'PipesImpl/Config.php';
require_once 'PipesImpl/FileSystem/Helper/Finder.php';

class Pipes_FileSystem_Helper_Finder implements PipesImpl_FileSystem_Helper_Finder
{
    private $_projects = array();
    private $_lastLoaded = '';

    public function __construct(PipesImpl_Config $config) {
        $this->_projects = array_reverse($config->projects);
//        var_dump($this->_projects);
    }

    public function find($name) {
        $classMinusNS = "Helpers_{$name}";
        $fileFinder = new Pipes_FileSystem_File_Finder();
        foreach ($this->_projects as $project) {
            try {
                $file = $fileFinder->findByClass("{$project}_{$classMinusNS}");
                $this->_lastLoaded = "{$project}_{$classMinusNS}";
                return $file;
            } catch (Pipes_Exception $e) {
                // try the next namespace
                $file = false;
            }
        }

        throw new Pipes_Exception("Unable to locate helper [{$name}]");
    }

    public function __get($key) {
        if ($key == 'lastLoaded') {
            return $this->_lastLoaded;
        }
    }
}
